﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace StaticView.Algorithm
{
    class LowPassFilter
    {
        public float CurrentValue { get; private set; }
        public float TimeConstant { get; private set; }
        public float TimeDifferentiation { get; private set; }

        float a;

        public LowPassFilter(float initValue, float timeConst, float dt)
        {
            CurrentValue = initValue;
            TimeConstant = timeConst;
            TimeDifferentiation = dt;

            a = TimeConstant / (TimeConstant + TimeDifferentiation);
        }

        public float Next(float newValue)
        {
            CurrentValue = (1 - a) * newValue + a * CurrentValue;
            return CurrentValue;
        }
    }

    class HighPassFilter
    {
        public float CurrentValue { get; private set; }
        public float TimeConstant { get; private set; }
        public float TimeDifferentiation { get; private set; }

        float a;
        float oldValue;

        public HighPassFilter(float initValue, float timeConst, float dt)
        {
            CurrentValue = initValue;
            TimeConstant = timeConst;
            TimeDifferentiation = dt;

            a = TimeConstant / (TimeConstant + TimeDifferentiation);
            oldValue = initValue;
        }

        public float Next(float newValue)
        {
            CurrentValue = a * (CurrentValue + newValue - oldValue);
            oldValue = newValue;
            return CurrentValue;
        }
    }
}
